package com.example.employ.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.employ.dao.entity.BoardDO;
import com.example.employ.dao.entity.CompaniesDO;
import com.example.employ.dao.mapper.BoardMapper;
import com.example.employ.dto.response.BoardDetailRespDTO;
import com.example.employ.dto.response.BoardListRespDTO;
import com.example.employ.service.BoardService;
import com.example.employ.service.CompaniesService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
@RequiredArgsConstructor
public class BoardServiceImpl extends ServiceImpl<BoardMapper, BoardDO> implements BoardService {

    private final CompaniesService companiesService;

    /**
     * 查询公告列表
     * @return
     */
    @Override
    public List<BoardListRespDTO> boardList() {
        // 查询发布了公告的列表，那么没删除的都要显示（公司，标题）
        LambdaQueryWrapper<BoardDO> queryWrapper = Wrappers.lambdaQuery(BoardDO.class)
                .eq(BoardDO::getDelFlag, 0);
        List<BoardDO> boardDOS = baseMapper.selectList(queryWrapper);
        // 获得各个公司的id然后查公司表查公司名
        List<BoardListRespDTO> result = new ArrayList<>();
        for (BoardDO boardDO : boardDOS) {
            CompaniesDO companiesDO = companiesService.getById(boardDO.getCompanyId());
            BoardListRespDTO boardListRespDTO = new BoardListRespDTO();
            boardListRespDTO.setTitle(boardDO.getTitle());
            boardListRespDTO.setId(boardDO.getId());
            boardListRespDTO.setContent(boardDO.getContent());
            boardListRespDTO.setCompanyName(companiesDO.getName());
            result.add(boardListRespDTO);
        }
        return result;
    }
}
